<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <th:block th:include="common/head::head"></th:block>
    <style>
        .context {
            height: 90vh;
        }
    </style>
</head>

<body>

<div class="layui-row">
    <div id="treeData" class="layui-col-md3"></div>
    <div id="log" class="layui-col-md9 logConsole">
        <div>当前查看：<span id="nowFileName"></span></div>
        <textarea name="context" id="logContext" placeholder="选择查看的文件" readonly disabled
                  class="layui-textarea context"></textarea>
    </div>
</div>
</body>
<script type="text/javascript"
        th:src="@{${session.jpomProxyPath}+'/static/js/auto-line-number.js?v='+${session.staticCacheTime}}"></script>
<script type="text/javascript">
    var treeData = [];
    var tree;
    var nodeId = "system";
    if ([[${node!=null?true:false}]]) {
        nodeId = "[[${node?.id}]]";
    }
    var url = getSocketHost() + "/tomcat_log?userId=[[${session.user.getUserMd5Key()}]]&tomcatId=system&nodeId=" + nodeId + "&type=tomcat";
    var ws;

    function loadSuccess() {
        var logContext = $("#logContext");
        initPlugin();
        layui.use('tree', function () {
            tree = layui.tree;
            //渲染
            var treeDom = tree.render({
                elem: '#treeData',
                data: [],
                id: "treeData",
                edit: ['del', 'add'],
                click: function (obj) {
                    if (obj.data.children) {
                        return;
                    }
                    $("#nowFileName").attr("path", obj.data.path);
                    $("#nowFileName").text(obj.data.title);
                    openSocket(obj.data.path);
                },
                operate: function (obj) {
                    var type = obj.type;
                    var data = obj.data;
                    if (type === 'del') {
                        layer.confirm('确定要删除此日志文件吗？', {
                            title: '系统提示'
                        }, function (index) {
                            layer.close(index);
                            loadingAjax({
                                url: './log_del.json',
                                data: {
                                    path: data.path,
                                    nodeId: "[[${node?.id}]]"
                                },
                                success: function (data) {
                                    layer.msg(data.msg);
                                    if (200 == data.code) {
                                        var attr = $("#nowFileName").attr("path");
                                        if (attr == data.path) {
                                            setTimeout(function () {
                                                location.reload();
                                            }, 2000)
                                        }
                                    } else {
                                        reloadTree();
                                    }
                                }
                            });
                        }, function () {
                            reloadTree();
                        });
                    } else if (type === 'add') {
                        if (data.children) {
                            layer.msg("不支持下载文件夹");
                        } else {
                            window.open("./log_download?path=" + data.path + "&nodeId=[[${node?.id}]]");
                        }
                        reloadTree();
                        return false;
                    }
                }
            });
            getData();
        });

        function openSocket(fileName) {
            if (!fileName) {
                return;
            }
            if (!('WebSocket' in window)) {
                layer.msg("不支持WebSocket");
                return;
            }
            logContext.html("");
            if (!ws) {
                ws = new WebSocket(url);
            }
            var item = {
                op: "showlog",
                tomcatId: 'system',
                fileName: fileName
            }
            if (ws.readyState != 1 && ws.readyState != 0) {
                ws = new WebSocket(url);
            } else if (ws.readyState == 1) {
                ws.send(JSON.stringify(item));
            }
            ws.onopen = function () {
                ws.send(JSON.stringify(item));
            };
            ws.onmessage = function (data) {
                if (data.data) {
                    logContext.append(data.data + '\r\n');
                }
                $("textarea").setTextareaCount({
                    width: "30px",
                    bgColor: "#ddd",
                    color: "#000",
                    display: "inline-block"
                });
                var h = logContext[0].scrollHeight;
                logContext.scrollTop(h);
            };
            ws.onerror = function (ev) {
                console.log(ev);
                layer.msg("socket 异常");
            }
            // scrollToBotomm();
        }
    }


    function getData() {
        loadingAjax({
            url: './log_data.json',
            data: {},
            success: function (data) {
                if (200 != data.code) {
                    layer.msg(data.msg);
                }
                treeData = data.data;
                reloadTree();
            }
        });
    }

    function reloadTree() {
        tree.reload('treeData', {
            data: treeData
        });
    }
</script>
</html>
